package com.ftg.learn.chapter14;

import java.sql.*;

/**
 * 优化一下代码，多少方面
 * driver close()
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/3/6 下午1:09
 */
public class LearnJdbc02 {

    /**
     * AutoCloseable 自动关闭
     */
    public static void main(String[] args) {
        String ip = "localhost";
        int port = 10086;
        String username = "root";
        String password = "123456";
        String url = "jdbc:mysql://" + ip + ":" + port + "/homework?serverTimezone=Asia/Harbin";
        String name = "s001";
        String sql = "select * from student where sno = '" + name+"'";
        try (
                Connection conn = DriverManager.getConnection(url, username, password);
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(sql);
        ) {
            while (rs.next()) {
                String sno = rs.getString(1);
                String sname = rs.getString(2);
                int age = rs.getInt(3);
                String sex = rs.getString(4);

                System.out.printf("编号为%s\t;姓名为%s\t;年龄为%d\t;性别为%s\n", sno, sname, age, sex);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


}
